package cn.lg.soar.database.base.service.impl;

import cn.lg.soar.common.util.AssertUtil;
import cn.lg.soar.database.base.entity.ITreeEntity;
import cn.lg.soar.database.base.service.ITreeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.io.Serializable;
import java.util.Collection;

/**
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public abstract class TreeServiceImpl<ID extends Serializable, M extends BaseMapper<T>, T extends ITreeEntity<ID>>
        extends BaseServiceImpl<M, T>
        implements ITreeService<ID, T> {

    @Override
    public boolean removeById(Serializable id) {
        QueryWrapper<T> wrapper = Wrappers.<T>query()
                .eq(T.PARENT_ID, id);
        boolean exist = exist(wrapper);
        AssertUtil.isFalse(exist, "请先删除子节点");
        return super.removeById(id);
    }

    @Override
    public boolean removeByIds(Collection<?> list) {
        QueryWrapper<T> wrapper = Wrappers.<T>query()
                .in(T.PARENT_ID, list);
        boolean exist = exist(wrapper);
        AssertUtil.isFalse(exist, "请先删除子节点");
        return super.removeBatchByIds(list);
    }

}
